/******************************************************************
 *
 *    Java Lib For JavaWeb, Powered By Shenzhen WEI YI KE JI .
 *
 *    Copyright (c) 2017-2027 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.theyeasy.dvmini.service.impl
 *
 *    Filename:    ArticlesServiceImpl.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2017-2027
 *
 *    Company:     Theyeasy Telemedia Co.,Ltd
 *
 *    @author:     hm
 *
 *    @version:    1.0.0
 *
 *    Create at:   2017年11月22日 上午10:13:53
 *
 *    Revision:
 *
 *    2017年11月22日 上午10:13:53
 *        - first revision
 *
 *****************************************************************/
package com.theyeasy.dvmini.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;
import com.theyeasy.dvmini.dao.ArticlesDao;
import com.theyeasy.dvmini.model.Articles;
import com.theyeasy.dvmini.service.ArticleService;
import com.theyeasy.dvmini.util.CommonUtil;
import com.theyeasy.dvmini.util.PageResults;
import com.theyeasy.dvmini.util.StringUtil;

import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName ArticlesServiceImpl
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author hm
 * @Date 2017年11月22日 上午10:13:53
 * @version 1.0.0
 */
@Service
public class ArticlesServiceImpl implements ArticleService{

    @Autowired
    ArticlesDao articleDao;
    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ArticleService#getArticlesList(java.lang.Integer, java.lang.Integer, java.lang.String)
     */
    @Override
    public PageResults<Articles> getArticlesList(Integer page, Integer pagesize, String keyword) {
        if(StringUtil.isZearoOrNull(pagesize))pagesize=10;
        Example example = new Example(Articles.class);
        example.createCriteria().andEqualTo("deleted", false);
        RowBounds row = new RowBounds((page - 1) * pagesize, pagesize);
        if(!Strings.isNullOrEmpty(keyword)){
            example.createCriteria().andLike("title", "%"+keyword+"%");
        }
        example.setOrderByClause(" id desc ");
        List<Articles> list = articleDao.selectByExampleAndRowBounds(example, row);
        int total = articleDao.selectCountByExample(example);
        PageResults<Articles> pageresult = new PageResults<>();
        pageresult.setPage(page);
        pageresult.setTotal(total);
        pageresult.setRows(list);
        pageresult.setPageSize(pagesize);
        return pageresult;
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ArticleService#addArticle(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public String addArticle(String title, String content, String imgurl) {
        // TODO Auto-generated method stub
        Articles article=new Articles();
        article.setTitle(title);
        article.setContent(content);
        article.setImgurl(imgurl);
        article.setCreatedtime(CommonUtil.getTime());
        article.setDeleted(false);
        article.setViewcount(0);
        int row = articleDao.insertSelective(article);
        return row > 0 ? "ok" : "添加失败，数据异常";
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ArticleService#updateArticle(java.lang.Integer, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public String updateArticle(Integer id, String title, String content, String imgurl) {
        // TODO Auto-generated method stub
        Articles article=articleDao.selectByPrimaryKey(id);
        if(article==null)return "修改失败，数据不存在";
        article.setTitle(title);
        article.setContent(content);
        article.setImgurl(imgurl);
        int row = articleDao.updateByPrimaryKeySelective(article);
        return row > 0 ? "ok" : "修改失败，数据异常";
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ArticleService#deleteArticle(java.lang.Integer)
     */
    @Override
    public String deleteArticle(Integer id) {
        Articles article=articleDao.selectByPrimaryKey(id);
        if(article==null)return "删除失败，数据不存在";
        article.setDeleted(true);
        int row = articleDao.updateByPrimaryKeySelective(article);
        return row > 0 ? "ok" : "删除失败，数据异常";
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ArticleService#getArticle(java.lang.Integer)
     */
    @Override
    public Articles getArticle(Integer id) {
        return articleDao.selectByPrimaryKey(id);
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ArticleService#addArticleViewCount(java.lang.Integer)
     */
    @Override
    public void addArticleViewCount(Integer id) {
        Articles article=articleDao.selectByPrimaryKey(id);
        if(article!=null){
            int viewcount=article.getViewcount();
            article.setViewcount(viewcount+1);
            articleDao.updateByPrimaryKeySelective(article);
        }
    }

}
